﻿var app = app || {};
(function () {

    app.utils = app.utils || {};

    app.utils.truncateString = function (str, maxLength, postfix) {
        if (!str || !maxLength || str.length <= maxLength) {
            return str;
        }

        if (postfix === false) {
            return str.substr(0, maxLength);
        }

        return str.substr(0, maxLength - 1) + '&#133;';
    };

    app.utils.removeCookie = function (key) {
        document.cookie = key + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    };

    //随机数
    app.utils.randomNum = function (min, max) {
        var range = max - min;
        var rand = Math.random();
        return min + Math.round(rand * range);
    };

    app.utils.toCamelCase = function (str) {
        if (str) {
            if (str.length === 1) {
                return str.toLowerCase();
            }

            return str.substr(0, 1).toLowerCase() + str.substring(1);
        }
        return str;
    };

    // 文件上传
    app.utils.file = app.utils.file || {};
    app.utils.file.getIcon = function (exName) {
        exName = exName.toLowerCase();
        if ("jpg|jpeg|gif|bmp|png|".indexOf(exName + "|") == -1) {
            if (exName === "zip" || exName === "rar" || exName === "tar"
                || exName === "gz" || exName === "gzip" || exName === "7z") {
                return "fa fa-fw fa-file-archive-o";
            } else if (exName === "pdf") {
                return "fa fa-fw fa-file-pdf-o";

            } else if (exName === "doc" || exName === "docx") {
                return "fa fa-fw fa-file-word-o";

            } else if (exName === "xls" || exName === "xlsx") {
                return "fa fa-fw fa-file-excel-o";

            } else if (exName === "ppt" || exName === "pptx") {
                return "fa fa-fw fa-file-powerpoint-o";
            } else if (exName === "txt" || exName === "jnt") {
                return "fa fa-fw fa-file-text";

            } else if (exName === "wav" || exName === "mp3" || exName === "ram"
                || exName === "wma" || exName === "mmf" || exName === "aac") {
                return "fa fa-fw fa-file-audio-o";

            } else {
                return "fa fa-fw fa-file";
            }
        }
        return null;
    };

    // 检测文件类型
    app.utils.file.checkExName = function (file, allowedExNames) {
        var fileExName = file.name.substring(file.name.lastIndexOf(".") + 1).toLowerCase();

        if ((allowedExNames + '|').indexOf(fileExName + "|") != -1) {
            return true;
        }

        var exNameArr = allowedExNames.split('|');
        abp.message.error(app.localize('File_Invalid_Type_Error', exNameArr.join('、')));
        return false;
    };

    // 检测最大值
    app.utils.file.checkMaxSize = function (file, allowedMaxSize) {
        var tempSize = Math.round(file.size / (1024 * 1024) * 100) / 100;
        if (allowedMaxSize >= tempSize) {
            return true;
        }
        abp.message.error(app.localize('File_SizeLimit_Error', allowedMaxSize, tempSize));
        return false;

    };

    // 文件大小
    app.utils.file.fileSize = function (size) {
        var tempSize = Math.round(size / 1024 * 100) / 100;
        if (tempSize > 1024) {
            return Math.round(tempSize / 1024) + "MB";
        }
        else {
            return tempSize + "KB";
        }
    };
    app.utils.file.check = function (file, allowedExNames, allowedMaxSize) {
        if (app.utils.file.checkExName(file, allowedExNames) && app.utils.file.checkMaxSize(file, allowedMaxSize)) {
            return true;
        }
        return false;
    };

    app.utils.file.getUrl = function (item) {
        if (item == null) {
            return '';
        }
        if (item.exName === 'pdf') {
            return '/' + item.fileUrl;
        }
        return '/Handler/DownAsync.ashx?myfile=' + item.fileUrl;
    };
    app.utils.file.getExName = function (fileUrl) {
        var index1 = fileUrl.lastIndexOf(".");
        var index2 = fileUrl.length;
        return fileUrl.substring(index1 + 1, index2);
    };
})();